package com.byx.chatclient.view.activity.user;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.Toast;

import com.byx.chatclient.R;
import com.byx.chatclient.contract.user.ForgetPwdTaskContract;
import com.byx.chatclient.presenter.user.ForgetPwdPresenter;
import com.byx.chatclient.view.activity.BaseActivity;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.BindView;
import butterknife.OnClick;
import soup.neumorphism.NeumorphButton;
import soup.neumorphism.NeumorphImageView;

/**
 * @author Superb
 */
public class ForgetPwdActivity extends BaseActivity<ForgetPwdPresenter> implements ForgetPwdTaskContract.View {

    @BindView(R.id.ll_register)
    protected TableLayout mTlRegForm;
    @BindView(R.id.reset_pwd)
    protected LinearLayout mLlChangePwd;
    @BindView(R.id.anim_btn_next_step)
    protected NeumorphButton mAnimBtnNext;
    @BindView(R.id.iv_back)
    protected NeumorphImageView mIvBack;
    @BindView(R.id.btn_verification)
    protected Button mBtnVerify;
    @BindView(R.id.et_account)
    protected EditText mEtAccount;
    @BindView(R.id.et_verification)
    protected EditText mEtVerification;
    @BindView(R.id.et_forget_password)
    protected EditText mEtNewPassword;

    private String email;
    private boolean mReadyToChangePwd =false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected ForgetPwdPresenter createPresenter() {
        return new ForgetPwdPresenter();
    }

    @Override
    protected void setRootView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_pwd);
    }

    @Override
    protected void initialize() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarDarkFont(true).init();
    }

    @Override
    protected void setListeners() {

    }

    @OnClick({R.id.btn_verification,R.id.anim_btn_next_step,R.id.iv_back})
    protected void onClicked(View view) {
        switch (view.getId()){
            case R.id.btn_verification:
//                Toast.makeText(ForgetPwdActivity.this,"验证码为123456，请勿泄露，如非本人，" +
//                    "请忽略此消息。（手动滑稽:-）",Toast.LENGTH_LONG).show();
                presenter.getVerificationCode();
                break;
            case R.id.anim_btn_next_step:
                if(mReadyToChangePwd){
                    Toast.makeText(ForgetPwdActivity.this,"提交新密码ing...",Toast.LENGTH_SHORT).show();
                    presenter.forgetPwd();
                    Toast.makeText(ForgetPwdActivity.this,"新密码更改成功！",Toast.LENGTH_SHORT).show();
                }else {
                    presenter.checkVerificationCode();
                }
                break;
            case R.id.iv_back:
                onBack();
                break;
            default:break;
        }
    }

    private void onBack(){
        if(mReadyToChangePwd){
            mTlRegForm.setVisibility(View.VISIBLE);
            mLlChangePwd.setVisibility(View.GONE);
            mReadyToChangePwd =false;
            mAnimBtnNext.setText("下一步");
        }else{
            finish();
        }
    }

    /**
     * 发送验证码成功
     */
    private void sendVerificationCodeSuccess() {
        new CountDownTimer(60*1000,1000){

            /**
             * Callback fired on regular interval.
             *
             * @param millisUntilFinished The amount of time until finished.
             */
            @Override
            public void onTick(long millisUntilFinished) {
                if (mBtnVerify != null) {
                    mBtnVerify.setEnabled(false);
                    mBtnVerify.setText("已发送（"+millisUntilFinished/1000+"s)");
                }
            }

            /**
             * Callback fired when the time is up.
             */
            @Override
            public void onFinish() {
                if (mBtnVerify != null) {
                    mBtnVerify.setEnabled(true);
                    mBtnVerify.setText("重新发送");
                }
            }
        }.start();
    }

    /**
     * Called when a key was released and not handled by any of the views
     * inside of the activity. So, for example, key presses while the cursor
     * is inside a TextView will not trigger the event (unless it is a navigation
     * to another object) because TextView handles its own key presses.
     *
     * <p>The default implementation handles KEYCODE_BACK to stop the activity
     * and go back.
     *
     * @param keyCode
     * @param event
     * @return Return <code>true</code> to prevent this event from being propagated
     * further, or <code>false</code> to indicate that you have not handled
     * this event and it should continue to be propagated.
     * @see #onKeyDown
     * @see KeyEvent
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP){
            onBack();
            //不执行父类点击事件
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    /**
     * 获取登录账号
     *
     * @return 账号
     */
    @Override
    public String getAccount() {
        return mEtAccount.getText().toString().trim();
    }

    /**
     * 获取验证码
     *
     * @return 验证码
     */
    @Override
    public String getVerificationCode() {
        return mEtVerification.getText().toString().trim();
    }

    /**
     * 暂时保存email到view
     *
     * @param email
     */
    @Override
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * 从view获取email
     *
     * @return
     */
    @Override
    public String getEmail() {
        return email;
    }

    /**
     * 获取密码
     *
     * @return 密码
     */
    @Override
    public String getPassword() {
        return mEtNewPassword.getText().toString().trim();
    }

    /**
     * 错误时view层显示错误信息
     *
     * @param errorMsg
     * @param code
     */
    @Override
    public void onError(String errorMsg, int code) {
        toastSlow(errorMsg + "(" + code + ")");
    }

    /**
     * 错误时view层显示错误信息
     *
     * @param errorMsg
     */
    @Override
    public void onError(String errorMsg) {
        toastSlow(errorMsg);
    }

    /**
     * 成功时调用view层方法
     */
    @Override
    public void onSuccess() {
        finish();
    }

    /**
     * 发送验证码成功
     */
    @Override
    public void onSendVerificationCodeSuccess() {
        sendVerificationCodeSuccess();
    }

    /**
     * 验证码检验成功
     */
    @Override
    public void onCheckVerificationCodeSuccess() {
        mTlRegForm.setVisibility(View.GONE);
        mLlChangePwd.setVisibility(View.VISIBLE);
        mReadyToChangePwd =true;
        mAnimBtnNext.setText("完成");
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}